package com.neu.af.dataFilter.service.impl;

import com.neu.af.dataFilter.entity.DataStats;
import com.neu.af.dataFilter.entity.DataStatsDay;
import com.neu.af.dataFilter.entity.Jizu;
import com.neu.af.dataFilter.entity.pojo.*;
import com.neu.af.dataFilter.mapper.AbDataMapper;
import com.neu.af.dataFilter.mapper.DataStatsDayMapper;
import com.neu.af.dataFilter.mapper.DataStatsMapper;
import com.neu.af.dataFilter.mapper.JizuMapper;
import com.neu.af.dataFilter.service.DataStatsService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service("DataStatsService")
public class DataStatsServiceImpl implements DataStatsService {
    @Autowired
    DataStatsMapper dataStatsMapper;
    @Autowired
    DataStatsDayMapper dataStatsDayMapper;
    @Autowired
    JizuMapper jizuMapper;
    @Autowired
    AbDataMapper abDataMapper;

    @Override
    public AllCount getAllCount() {
        AllCount allCount = new AllCount();

        if (dataStatsMapper.getAllCount() == 0){
            allCount.setTotalAlarmNum(0);
    }
        else{
            allCount.setTotalAlarmNum(dataStatsMapper.getAllCount());

        }
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM");
        String datetime = tempDate.format(new Date());
        List<DataStats> dataStatsList = dataStatsMapper.searchBydatetime(datetime);
        if(dataStatsList.size()==0){
            allCount.setMonthAlarmNum(0);
            allCount.setClassoneAlarmNum(0);
            allCount.setClasstwoAlarmNum(0);
        }else{
            allCount.setMonthAlarmNum(dataStatsList.get(0).getAllnum());
            allCount.setClassoneAlarmNum(dataStatsList.get(0).getClassonenum());
            allCount.setClasstwoAlarmNum(dataStatsList.get(0).getClasstwonum());
        }

        return allCount;
    }

    @Override
    public List<OneWeekCount> getOneWeekCount() {
        List<OneWeekCount> oneWeekCounts = new ArrayList<>();
        OneWeekCount oneWeekCount1 = new OneWeekCount();
        OneWeekCount oneWeekCount2 = new OneWeekCount();
        oneWeekCount1.setAlarmClass("1");
        oneWeekCount2.setAlarmClass("2");
        Map<String, Integer> oneClassIntegers = new LinkedHashMap<>();
        Map<String, Integer> twoClassIntegers = new LinkedHashMap<>();

        for(int i=0;i<=6;i++){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DATE, - i);
            Date d = c.getTime();
            String day = format.format(d);
            List<DataStatsDay> dataStatsDays = dataStatsDayMapper.searchDataStatsDayBydatetime(day);
            if(dataStatsDays.size() == 0){
                break;
            }else{
                format = new SimpleDateFormat("EEEE");
                Calendar XQc = Calendar.getInstance();
                XQc.setTime(new Date());
                XQc.add(Calendar.DATE, - i);
                Date XQd = XQc.getTime();
                String XQday = format.format(XQd);
                oneClassIntegers.put(XQday,dataStatsDays.get(0).getClassonenum());
                twoClassIntegers.put(XQday,dataStatsDays.get(0).getClasstwonum());
            }
        }

        oneWeekCount1.setAlarmCountEveryDay(oneClassIntegers);
        oneWeekCount2.setAlarmCountEveryDay(twoClassIntegers);
        oneWeekCounts.add(oneWeekCount1);
        oneWeekCounts.add(oneWeekCount2);
        return oneWeekCounts;


    }

    @Override
    public List<JizuAlarmCount> getAlarmCountByJizu() {
        List<JizuAlarmCount> jizuAlarmCounts = new ArrayList<>();
        List<String> jizunameList = jizuMapper.getAllJizuName();
        if (jizunameList.size()!=0){
            for (int i = 0;i<jizunameList.size();i++){
                SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM");
                Calendar ccc = Calendar.getInstance();
                ccc.setTime(new Date());
                ccc.add(Calendar.MONTH, - 1);
                Date d = ccc.getTime();
                String datetime = tempDate.format(d);
                List<Jizu> jizuList = jizuMapper.searchByName(jizunameList.get(i),datetime);
                if (jizuList.size()==0){
                    break;
                }else{
                    JizuAlarmCount jizuAlarmCount = new JizuAlarmCount();
                    jizuAlarmCount.setAlarmName(jizuList.get(0).getJizuname());
                    jizuAlarmCount.setOneClassAlarmNum(jizuList.get(0).getClassonenum());
                    jizuAlarmCount.setTwoClassAlarmNum(jizuList.get(0).getClasstwonum());

                    //计算去年同期
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
                    Calendar c = Calendar.getInstance();
                    c.setTime(new Date());
                    c.add(Calendar.MONTH, - 1);
                    c.add(Calendar.YEAR, - 1);
                    Date dd = c.getTime();
                    String year = format.format(dd);
                    List<Jizu> jizuListByyear = jizuMapper.searchByName(jizunameList.get(i),year);
                    if (jizuListByyear.size()==0){
                        jizuAlarmCount.setOneClassYoY(0);
                        jizuAlarmCount.setTwoClassYoY(0);
                    }else{
                        if (jizuListByyear.get(0).getClassonenum() == 0){
                            jizuAlarmCount.setOneClassYoY(0);
                        }else{
                            jizuAlarmCount.setOneClassYoY(((float)jizuList.get(0).getClassonenum()-(float)jizuListByyear.get(0).getClassonenum())/(float)jizuListByyear.get(0).getClassonenum());
                        }
                        if(jizuListByyear.get(0).getClasstwonum() == 0){
                            jizuAlarmCount.setTwoClassYoY(0);
                        }else{
                            jizuAlarmCount.setTwoClassYoY(((float)jizuList.get(0).getClasstwonum()-(float)jizuListByyear.get(0).getClasstwonum())/(float)jizuListByyear.get(0).getClasstwonum());
                        }

                    }

                   //计算上月同期
                    format = new SimpleDateFormat("yyyy-MM");
                    Calendar cc = Calendar.getInstance();
                    cc.setTime(new Date());
                    cc.add(Calendar.MONTH, - 2);
                    d = cc.getTime();
                    String month = format.format(d);
                    List<Jizu> jizuListBymonth = jizuMapper.searchByName(jizunameList.get(i),month);
                    if (jizuListBymonth.size()==0){
                        jizuAlarmCount.setOneClassMoM(0);
                        jizuAlarmCount.setTwoClassMoM(0);
                    }else{
                        if (jizuListBymonth.get(0).getClassonenum() == 0){
                            jizuAlarmCount.setOneClassMoM(0);
                        }else{
                            jizuAlarmCount.setOneClassMoM(((float)jizuList.get(0).getClassonenum()-(float)jizuListBymonth.get(0).getClassonenum())/(float)jizuListBymonth.get(0).getClassonenum());
                        }
                        if(jizuListBymonth.get(0).getClasstwonum() == 0){
                            jizuAlarmCount.setTwoClassMoM(0);
                        }else{
                            jizuAlarmCount.setTwoClassMoM(((float)jizuList.get(0).getClasstwonum()-(float)jizuListBymonth.get(0).getClasstwonum())/(float)jizuListBymonth.get(0).getClasstwonum());
                        }

                    }
                    jizuAlarmCounts.add(jizuAlarmCount);
                }
            }
        }
        return jizuAlarmCounts;
    }

    @Override
    public CompareAlarmCount getCompareAlarmCount() {
        CompareAlarmCount cac = new CompareAlarmCount();
        //先查出上一个月的数据
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, - 1);
        Date dd = c.getTime();
        String dateTime = format.format(dd);
        List<DataStats> dataStatsList = dataStatsMapper.searchBydatetime(dateTime);
        if(dataStatsList.size() == 0)
        {
            //如果上一个月的数据没有，那么就不用算了，直接返回0
            cac.setOneClassMoM(0.0);
            cac.setOneClassYoY(0.0);
            cac.setTwoClassMoM(0.0);
            cac.setTwoClassYoY(0.0);
            return cac;
        }
        //计算去年上月同期（同比）
        c.add(Calendar.YEAR, - 1);
        dd = c.getTime();
        dateTime = format.format(dd);
        List<DataStats> dataStatsListLastYear = dataStatsMapper.searchBydatetime(dateTime);
        if(dataStatsListLastYear.size() == 0)
        {
            //如果去年的数据没有，那么同比的数据不用算了，直接设置为0
            cac.setOneClassYoY(0.0);
            cac.setTwoClassYoY(0.0);
        }
        else
        {
            DataStats dataStats = dataStatsList.get(0);
            DataStats dataStatsLastYear = dataStatsListLastYear.get(0);
            if(dataStatsLastYear.getClassonenum() == 0) cac.setOneClassYoY(0.0);
            else {
                cac.setOneClassYoY((dataStats.getClassonenum() - dataStatsLastYear.getClassonenum())
                                /(1.0*dataStatsLastYear.getClassonenum()));
            }
            if(dataStatsLastYear.getClasstwonum() == 0) cac.setTwoClassYoY(0.0);
            else {
                cac.setTwoClassYoY((dataStats.getClasstwonum() - dataStatsLastYear.getClasstwonum())
                        /(1.0*dataStatsLastYear.getClasstwonum()));
            }

        }
        //计算上上月同期（环比）
        c.add(Calendar.YEAR, +1);
        c.add(Calendar.MONTH,-1);
        dd = c.getTime();
        dateTime = format.format(dd);
        List<DataStats> dataStatsListLastMonth = dataStatsMapper.searchBydatetime(dateTime);
        if(dataStatsListLastMonth.size() == 0)
        {
            //如果去年的数据没有，那么同比的数据不用算了，直接设置为0
            cac.setOneClassMoM(0.0);
            cac.setTwoClassMoM(0.0);
        }
        else
        {
            DataStats dataStats = dataStatsList.get(0);
            DataStats dataStatsLastMonth = dataStatsListLastMonth.get(0);
            if(dataStatsLastMonth.getClassonenum() == 0) cac.setOneClassMoM(0.0);
            else {
                cac.setOneClassMoM((dataStats.getClassonenum() - dataStatsLastMonth.getClassonenum())
                        /(1.0*dataStatsLastMonth.getClassonenum()));
            }
            if(dataStatsLastMonth.getClasstwonum() == 0) cac.setTwoClassMoM(0.0);
            else {
                cac.setTwoClassMoM((dataStats.getClasstwonum() - dataStatsLastMonth.getClasstwonum())
                        /(1.0*dataStatsLastMonth.getClasstwonum()));
            }

        }
        return cac;
    }

    @Override
    public List<RankAlarmCount> getRankAlarmCount() {
        List<RankAlarmCount> rankData = abDataMapper.getRankData();
        return rankData;
    }
}
